// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PlaneTest.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Test.Robotics.Geometry
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    using Microsoft.Robotics.Geometry;
    using Microsoft.Robotics.Numerics;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Unit test for the plane class
    /// </summary>
    [TestClass]
    public class PlaneTest
    {
        /// <summary>
        /// Test that the constructor works
        /// </summary>
        [TestMethod]
        public void PlaneConstructorTest()
        {
            new Plane(
            new Vector3(0, 0, 0),
            new Vector3(1, 1, 0),
            new Vector3(1, 3, 0));
        }

        /// <summary>
        /// Test that the constructor fails if a point is repeated
        /// </summary>
        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void PlaneConstructorNonUniquePointsTest()
        {
            new Plane(
            new Vector3(0, 0, 0),
            new Vector3(1, 1, 0),
            new Vector3(1, 1, 0));
        }

        /// <summary>
        /// Test that the constructor fails if 3-colinear points are given
        /// </summary>
        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void PlaneConstructorConlinearPointsTest()
        {
            new Plane(
            new Vector3(0, 0, 0),
            new Vector3(1, 1, 1),
            new Vector3(2, 2, 2));
        }

        /// <summary>
        /// Create a plane using the 3-point constructor and 
        /// confirm that projection of a point into the plane works
        /// as expected.
        /// </summary>
        [TestMethod]
        public void Plane3PointProjectionRandomTest()
        {
            int testIterations = 10000;
            int seed = 425722734;

            Random random = new Random(seed);

            for (int i = 0; i < testIterations; ++i)
            {
                Vector3 p1 = this.RandomVector3(random);
                Vector3 p2 = this.RandomVector3(random);
                Vector3 p3 = this.RandomVector3(random);

                Plane plane = new Plane(p1, p2, p3);

                Vector3 p4 = this.RandomVector3(random);

                // test that the projection of p4 into the plane is actually in the plane
                // both by trying to project it again and confirming same result is unchanged,
                // making sure that the cross product of (p1 - p4Projected) and (p2 - p4Projected) is parallel
                // to the plane normal, and making sure that we get the same plane back if we build a plane using
                // p1, p2, and p4Projected.
                Vector3 point4Projected = plane.ProjectPointToPlane(p4);

                Assert.AreEqual(point4Projected, plane.ProjectPointToPlane(point4Projected));

                Vector3 n = Vector3.Cross((p1 - point4Projected), (p2 - point4Projected));
                n = Vector3.Normalize(n);

                Assert.IsTrue(n == plane.Normal || -n == plane.Normal);

                Plane plane2 = new Plane(p1, p2, point4Projected);
                Assert.IsTrue(plane2.Normal == plane.Normal || -plane2.Normal == plane.Normal);
            }
        }

        /// <summary>
        /// Get a random Vector3
        /// </summary>
        /// <param name="random">Random number generator.</param>
        /// <returns>Random Vector3.</returns>
        private Vector3 RandomVector3(Random random)
        {
            return new Vector3(random.NextDouble(), random.NextDouble(), random.NextDouble());
        }
    }
}
